package com.company.cloud.oss.modules.app.controller;

import java.io.IOException;

import com.company.common.server.config.id.OssIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.oss.config.OssConfig;
import com.company.cloud.oss.constant.OssErrorEnum;
import com.company.cloud.oss.modules.app.controller.req.Upload2Base64Param;
import com.company.cloud.oss.modules.app.utils.FileNameUtils;
import com.company.cloud.oss.modules.base.entity.OssStoreInfo;
import com.company.cloud.oss.modules.base.service.OssStoreInfoService;
import com.company.cloud.oss.modules.cache.models.OssModel;
import com.company.cloud.oss.modules.cache.utils.OssConvertUtils;
import com.company.cloud.oss.modules.cloud.OSSFactory;

import cn.hutool.core.codec.Base64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Login
@RestController
@RequestMapping("/app/file")
@Api(tags="App_OSS_文件上传服务")
public class FileController {

	@Autowired
	private OssStoreInfoService ossStoreInfoService;
	@Autowired
	private OssConfig ossConfig;
	
	@PostMapping("/upload/file")
	@ApiOperation(value="上传文件(二进制)", response=OssModel.class)
	public BaseResult uploadFile(@RequestParam("file")final MultipartFile file) {
		LoginSimpleUserInfo userInfo =AuthContextUtils.getUser();
		if(file==null || file.isEmpty()) {
			 throw new BusinessException(BaseReturnEnum.PARAM_NULL.getCode(), "文件不能为空");
		}
		if(file.getSize()>10*1024*1024) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL.getCode(), "文件太大");
		}
		String fileFormat= FileNameUtils.getExtension(file.getOriginalFilename());
		String fileName=FileNameUtils.getFileName(FileNameUtils.getImgNo(),fileFormat);
		String url;
		try {
			url = OSSFactory.build(ossConfig).upload(file.getBytes(), fileName);
		} catch (IOException e) {
			e.printStackTrace();
			throw new BusinessException(OssErrorEnum.Oss_Upload_File_Error);
		}
		OssStoreInfo entity=new OssStoreInfo();
		entity.setFileNo(OssIdUtil.getFileNo());
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setFileName(fileName);
		entity.setFileUrl(url);
		entity.setFileFormat(fileFormat);
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		ossStoreInfoService.save(entity);
		OssModel data= OssConvertUtils.getOssModel(entity);
		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/upload/fileParams")
	@ApiOperation(value="上传文件(二进制)", response=OssModel.class)
	public BaseResult uploadFileParams(@RequestParam("file")final MultipartFile file, @RequestParam("width") String width,@RequestParam("height")String height,@RequestParam("aspect")String aspect) {
		LoginSimpleUserInfo userInfo =AuthContextUtils.getUser();
		if(file==null || file.isEmpty()) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL.getCode(), "文件不能为空");
		}
		if(file.getSize()>10*1024*1024) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL.getCode(), "文件太大");
		}
		String fileFormat= FileNameUtils.getExtension(file.getOriginalFilename());
		String fileName=FileNameUtils.getFileName(FileNameUtils.getImgNo(),fileFormat);
		String url;
		try {
			url = OSSFactory.build(ossConfig).upload(file.getBytes(), fileName);
		} catch (IOException e) {
			e.printStackTrace();
			throw new BusinessException(OssErrorEnum.Oss_Upload_File_Error);
		}
		if(!width.isEmpty()&&!height.isEmpty()&&!aspect.isEmpty()){
			url = url + "?w="+width+"&h="+height+"&a="+aspect;
		}
		OssStoreInfo entity=new OssStoreInfo();
		entity.setFileNo(OssIdUtil.getFileNo());
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setFileName(fileName);
		entity.setFileUrl(url);
		entity.setFileFormat(fileFormat);
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		ossStoreInfoService.save(entity);
		OssModel data= OssConvertUtils.getOssModel(entity);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/upload/fileBase64")
	@ApiOperation(value="上传文件(base64)", response=OssModel.class)
	public BaseResult uploadFileByBase64(@RequestBody Upload2Base64Param params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo =AuthContextUtils.getUser();
		String fileFormat= FileNameUtils.getExtension(params.getFileSuffix());
		String fileName=FileNameUtils.getFileName(FileNameUtils.getImgNo(),fileFormat);
		byte[] content= Base64.decode(params.getContent());
		String url=null;
		try {
			url = OSSFactory.build(ossConfig).upload(content, fileName);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(OssErrorEnum.Oss_Upload_File_Error);
		}
		OssStoreInfo entity=new OssStoreInfo();
		entity.setFileNo(OssIdUtil.getFileNo());
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setFileName(fileName);
		entity.setFileUrl(url);
		entity.setFileFormat(fileFormat);
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		ossStoreInfoService.save(entity);
		OssModel data= OssConvertUtils.getOssModel(entity);
		return BaseResult.ok(data);
	}
	
	
	@PostMapping("/upload/fileParamsBase64")
	@ApiOperation(value="上传文件(base64)", response=OssModel.class)
	public BaseResult uploadFileParamsByBase64(@RequestBody Upload2Base64Param params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo =AuthContextUtils.getUser();
		String fileFormat= FileNameUtils.getExtension(params.getFileSuffix());
		String fileName=FileNameUtils.getFileName(FileNameUtils.getImgNo(),fileFormat);
		byte[] content= Base64.decode(params.getContent());
		String url=null;
		try {
			url = OSSFactory.build(ossConfig).upload(content, fileName);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(OssErrorEnum.Oss_Upload_File_Error);
		}
		if(!params.getWidth().isEmpty()&&!params.getHeight().isEmpty()&&!params.getAspect().isEmpty()){
			url = url + "?w="+params.getWidth()+"&h="+params.getHeight()+"&a="+params.getAspect();
		}
		OssStoreInfo entity=new OssStoreInfo();
		entity.setFileNo(OssIdUtil.getFileNo());
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setFileName(fileName);
		entity.setFileUrl(url);
		entity.setFileFormat(fileFormat);
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		ossStoreInfoService.save(entity);
		OssModel data= OssConvertUtils.getOssModel(entity);
		return BaseResult.ok(data);
	}
	
	
	@GetMapping("/delete")
	@ApiOperation(value="删除文件", response=BaseResult.class)
	public BaseResult delete(@RequestParam("fileName")final String fileName) {
		if(StringUtils.isEmpty(fileName)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL.getCode(), "文件名不能为空");
		}
		OSSFactory.build(ossConfig).delete(fileName);
		return BaseResult.ok();
	}
	
	
}
